package ullio.utils
{
	import mx.utils.ArrayUtil;
	
	public class ArrayUtils 
	{
		public static function getIndexByID(source:Array,ID:String,value:*=null):int
		{
			
			var n:int = source.length;
	        for (var i:int = 0; i < n; i++)
	        {	
	            if ( source[i][ID] == value )
	                return i;
	        }
	
	        return -1;
		}
		
		
		public static function getFirstIndexPartialMatchByID(source:Array,ID:String,value:*=null):int
		{
			
			var n:int = source.length;
	        for (var i:int = 0; i < n; i++)
	        {	
	            if ( StringUtils.contains(source[i][ID],value) )
	                return i;
	        }
	
	        return -1;
		}
				
		
		public static function setByID(source:Array,item:Object,ID:String):Array
		{
			var index : int = getIndexByID(source,ID,item[ID]);
				
				if ( index == -1)
				{
					source.push(item);
				}
				else
				{
					source[index] = item;	
				}
			
			return source;
		}
		
		public static function getItemByIndex( item:Object,source:Array):int
		{
			return ArrayUtil.getItemIndex( item,source);
		}
		
		
	}
}